<?php

require_once('../include/GeneralTop.php');
require('../include/class/db.php');

if (!$content_admin) {
	die('<div style="text-align:center">Permission denied...</div>');
}

if ((isset($_POST["submit"]))) {
    $db = new db;
    
    //handle pics
    /*********************BEGIN*************************/
    $sub_folder = date("Y-m-d");	
	$savePath = IMAGES_PET_SAVE_FOLDER.'/'.$sub_folder;
	if(!is_dir($savePath)) {
		mkdir($savePath, 0755);
	}
    
    $fileTemp1 = @getimagesize($_FILES['pic1']['tmp_name']);
	$trueType1 = @$fileTemp1['mime'];
	$fileTemp2 = @getimagesize($_FILES['pic2']['tmp_name']);
	$trueType2 = @$fileTemp2['mime'];
	$fileTemp3 = @getimagesize($_FILES['pic3']['tmp_name']);
	$trueType3 = @$fileTemp3['mime'];
	$fileSizeAll1 = @$_FILES['pic1']['size'];
	$fileSizeAll2 = @$_FILES['pic2']['size'];
	$fileSizeAll3 = @$_FILES['pic3']['size'];
	if('' != $_FILES['pic1']['name']) {
		if(!in_array($trueType1, $typeArray)) {
			echo '<div align="center">The format of the pics you uploaded is not png,jpg,jpeg,gif, please upload again</div>';
			die();
		}
		if($fileSizeAll1 > $pic_size_limit * 1024) {
			echo '<div align="center">The size of the pics you uploaded exceed limitation, please upload another one</div>';
			die();
		}
		//first pic
		$fileId = md5($_FILES["pic1"]["tmp_name"] + rand()*100000);
		$imagePathInfo = pathinfo($_FILES['pic1']['name']);
		$extension = $imagePathInfo['extension'];
		$imageName = $fileId;
		$imageFullName = $savePath.'/'.$imageName.'.'.$extension;
		$file_name1 = '/'.$sub_folder.'/'.$imageName.'.'.$extension;
		move_uploaded_file($_FILES['pic1']['tmp_name'], $imageFullName);
		
		//generate thumb of first pic
		$imageName_t = $fileId.'_t';
		$imageFullName_t = $savePath.'/'.$imageName_t.'.'.$extension;
		$file_name1_t = '/'.$sub_folder.'/'.$imageName_t.'.'.$extension;
		generateImages($trueType1, $imageFullName, PIC_THUMB_WIDTH, PIC_THUMB_HEIGHT, $imageFullName_t);
	}
	if('' != $_FILES['pic2']['name']) {
		if(!in_array($trueType2, $typeArray)) {
			echo '<div align="center">The format of the pics you uploaded is not png,jpg,jpeg,gif, please upload again</div>';
			die();
		}
		if($fileSizeAll2 > $pic_size_limit * 1024) {
			echo '<div align="center">The size of the pics you uploaded exceed limitation, please upload another one</div>';
			die();
		}
		//second pic
		$fileId = md5($_FILES["pic2"]["tmp_name"] + rand()*100000);
		$imagePathInfo = pathinfo($_FILES['pic2']['name']);
		$extension = $imagePathInfo['extension'];
		$imageName = $fileId;
		$imageFullName = $savePath.'/'.$imageName.'.'.$extension;
		$file_name2 = '/'.$sub_folder.'/'.$imageName.'.'.$extension;
		move_uploaded_file($_FILES['pic2']['tmp_name'], $imageFullName);
	}
	if('' != $_FILES['pic3']['name']) {
		if(!in_array($trueType3, $typeArray)) {
			echo '<div align="center">The format of the pics you uploaded is not png,jpg,jpeg,gif, please upload again</div>';
			die();
		}
		if($fileSizeAll3 > $pic_size_limit * 1024) {
			echo '<div align="center">The size of the pics you uploaded exceed limitation, please upload another one</div>';
			die();
		}
		//third pic
		$fileId = md5($_FILES["pic3"]["tmp_name"] + rand()*100000);
		$imagePathInfo = pathinfo($_FILES['pic3']['name']);
		$extension = $imagePathInfo['extension'];
		$imageName = $fileId;
		$imageFullName = $savePath.'/'.$imageName.'.'.$extension;
		$file_name3 = '/'.$sub_folder.'/'.$imageName.'.'.$extension;
		move_uploaded_file($_FILES['pic3']['tmp_name'], $imageFullName);
	}	

	/*********************END**************************/
	
	
    //if(isset($_POST['doc_name']) && $_POST['doc_name'] != '') {
    //	$doctor_check = $_POST['doc_name'];
    //}
    //else {
    	$doctor_check = $_POST['doctor_check_yn'];
    //}
    $insertSQL = sprintf("INSERT INTO pet_info (PAPetName, PVariety, pet_origin, PADate, PAMemo, 
							PType, PGender, age, age_grp, spay_YN, spay_date, vaccinated_YN, vaccinated_date, dewormIn_YN,
							 dewormInDate, dewormExt_YN, dewormExtDate, Pet_healthinfo, Pet_otherinfo, Doctor_check,  
							 PPhoto_main, PPhoto_main_thumb, photo_detail1, photo_detail2, PIsAdopt, adiopted_date, 
							 adoption_history, PAAdopter, Adopter_sex,
							 Adopter_ID, Adopter_workplace, adopter_phone, adopter_address, Rescuer_name,
							 Rescuer_sex, Rescuer_phone, Rescuer_address, contact_way, reason_for_return, add_user, 
							 guid, arrive_date, story) 
							VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 
									%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
									%s, %s, %s)",
                       GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['PVariety'], "text"),
                       GetSQLValueString($_POST['origin'], "int"),
                       GetSQLValueString($_POST['res_date'], "text"),
                       GetSQLValueString($_POST['abundon_reason'], "text"),
                       GetSQLValueString($_POST['type'], "int"),
                       GetSQLValueString($_POST['pet_sex'], "text"),
                       GetSQLValueString($_POST['age'], "float"),
                       GetSQLValueString($_POST['age_grp'], "int"),
                       GetSQLValueString($_POST['spay_yn'], "text"),
                       GetSQLValueString($_POST['spay_date'], "text"),
                       GetSQLValueString($_POST['vaccinate_yn'], "text"),
                       GetSQLValueString($_POST['vaccinate_date'], "text"),
                       GetSQLValueString($_POST['Dewormin_yn'], "text"),
                       GetSQLValueString($_POST['Dewormin_date'], "text"),
                       GetSQLValueString($_POST['Dewormext_yn'], "text"),
                       GetSQLValueString($_POST['Dewormext_date'], "text"),
                       GetSQLValueString($_POST['health_info'], "text"),
                       GetSQLValueString($_POST['other_info'], "text"),
                       GetSQLValueString($doctor_check, "text"),
                       GetSQLValueString($file_name1, "text"),
                       GetSQLValueString($file_name1_t, "text"),
                       GetSQLValueString($file_name2, "text"),
                       GetSQLValueString($file_name3, "text"),
                       GetSQLValueString($_POST['PIsAdopt'], "text"),
                       GetSQLValueString($_POST['adopt_date'], "text"),
                       GetSQLValueString($_POST['adopt_history'], "text"),
                       GetSQLValueString($_POST['adopter_name'], "text"),
                       GetSQLValueString($_POST['adopter_sex'], "int"),
                       GetSQLValueString($_POST['adopter_id'], "text"),
                       GetSQLValueString($_POST['adopter_workplace'], "text"),
                       GetSQLValueString($_POST['adopter_phone'], "text"),
                       GetSQLValueString($_POST['adopt_address'], "text"),
                       GetSQLValueString($_POST['res_name'], "text"),
                       GetSQLValueString($_POST['res_sex'], "int"),
                       GetSQLValueString($_POST['res_phone'], "text"),
                       GetSQLValueString($_POST['res_address'], "text"),
                       GetSQLValueString($_POST['contact_way'], "text"),
                       GetSQLValueString($_POST['return_reason'], "text"),
                       GetSQLValueString($_SESSION['MM_Username_id'], "int"),
                       GetSQLValueString($_POST['guid'], "text"),
                       GetSQLValueString($_POST['arr_date'], "text"),
                       GetSQLValueString($_POST['story'], "text"));
	//echo $insertSQL;
    $db->query($insertSQL);
    header("location:".$_SERVER['PHP_SELF']."?result=suc");
}

include('AdminPanel.php');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="../include/js/calendar.js" type="text/javascript"></script>
</head>

<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post" name="form1" id="form1" onsubmit="return check_form();">
  <table align="center">
    <?php
    if($_GET['result'] && 'suc' == $_GET['result']) {
    ?>
    <tr valign="baseline">
      <td colspan="2" nowrap="nowrap" align="center"><font color="green">添加成功...</font></td>
    </tr>
    <?php
    }
    ?>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 宠物名:</td>
      <td><input type="text" name="name" id="name" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 编号:</td>
      <td><input type="text" name="guid" id="guid" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 品种:</td>
      <td><input type="text" name="PVariety" id="PVariety" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 宠物来源:</td>
      <td>
		<input type="radio" name="origin" value="1" checked> 流浪 
		<input type="radio" name="origin" value="2"> 家养 
	  </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 救助日期:</td>
      <td>
      	<input type="text" name="res_date" id="res_date" value="" size="32" />
      	<img src="../images/calendar.ico" width="25" height="25" onclick="fPopCalendar(event,this,document.getElementById('res_date'))" style="cursor:pointer;">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 到达分会日期:</td>
      <td>
      	<input type="text" name="arr_date" id="arr_date" value="" size="32" />
      	<img src="../images/calendar.ico" width="25" height="25" onclick="fPopCalendar(event,this,document.getElementById('arr_date'))" style="cursor:pointer;">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 弃养原因:</td>
      <td><input type="text" name="abundon_reason" id="abundon_reason" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 种类:</td>
      <td>
		<input type="radio" name="type" value="1" checked> 狗 
		<input type="radio" name="type" value="2"> 猫 
		<input type="radio" name="type" value="3"> 其他 
	  </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 性别:</td>
      <td>
		<input type="radio" name="pet_sex" value="1" checked> 雄 
		<input type="radio" name="pet_sex" value="2"> 雌 
	  </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 年龄:</td>
      <td><input type="text" name="age" id="age" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 年龄类别:</td>
      <td>
		<input type="radio" name="age_grp" value="1" checked> 年幼 
		<input type="radio" name="age_grp" value="2"> 成年 
	  </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 是否绝育:</td>
      <td>
		<input type="radio" name="spay_yn" value="Y"> 是 
		<input type="radio" name="spay_yn" value="N" checked> 否 
		<input type="radio" name="spay_yn" value="3"> 未知 
	  </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">绝育日期:</td>
      <td>
      	<input type="text" name="spay_date" id="spay_date" value="" size="32" />
      	<img src="../images/calendar.ico" width="25" height="25" onclick="fPopCalendar(event,this,document.getElementById('spay_date'))" style="cursor:pointer;">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 是否免疫:</td>
      <td>
		<input type="radio" name="vaccinate_yn" value="Y"> 是 
		<input type="radio" name="vaccinate_yn" value="N" checked> 否 
		<input type="radio" name="vaccinate_yn" value="3"> 未知 
	  </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">免疫日期:</td>
      <td>
      	<input type="text" name="vaccinate_date" id="vaccinate_date" value="" size="32" />
      	<img src="../images/calendar.ico" width="25" height="25" onclick="fPopCalendar(event,this,document.getElementById('vaccinate_date'))" style="cursor:pointer;">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 是否体内驱虫:</td>
      <td>
		<input type="radio" name="Dewormin_yn" value="Y"> 是 
		<input type="radio" name="Dewormin_yn" value="N" checked> 否 
		<input type="radio" name="Dewormin_yn" value="3"> 未知 
	  </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">体内驱虫日期:</td>
      <td>
      	<input type="text" name="Dewormin_date" id="Dewormin_date" value="" size="32" />
      	<img src="../images/calendar.ico" width="25" height="25" onclick="fPopCalendar(event,this,document.getElementById('Dewormin_date'))" style="cursor:pointer;">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 是否体外驱虫:</td>
      <td>
		<input type="radio" name="Dewormext_yn" value="Y"> 是 
		<input type="radio" name="Dewormext_yn" value="N" checked> 否 
		<input type="radio" name="Dewormext_yn" value="3"> 未知 
	  </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">体外驱虫日期:</td>
      <td>
      	<input type="text" name="Dewormext_date" id="Dewormext_date" value="" size="32" />
      	<img src="../images/calendar.ico" width="25" height="25" onclick="fPopCalendar(event,this,document.getElementById('Dewormext_date'))" style="cursor:pointer;">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right" valign="top"><font color="red">*</font> 健康情况:</td>
      <td><textarea name="health_info" id="health_info" rows="5" cols="30"></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right" valign="top"><font color="red">*</font> 性格和其他描述:</td>
      <td><textarea name="other_info" id="other_info" rows="5" cols="30"></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right" valign="top"><font color="red">*</font> 故事:</td>
      <td><textarea name="story" id="story" rows="5" cols="30"></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 是否接受医生诊断:</td>
      <td>
		<input type="radio" name="doctor_check_yn" value="Y"> 是 <!--<input type="text" name="doc_name" size="12"> (如有是，请填写医生姓名)-->
		<input type="radio" name="doctor_check_yn" value="N" checked> 否 
		<input type="radio" name="doctor_check_yn" value="3"> 未知 
	  </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><font color="red">*</font> 上传照片:</td>
      <td><input type="file" name="pic1" id="pic1"/> (列表页图，会自动生成缩略图)<br><input type="file" name="pic2" id="pic2" /><br><input type="file" name="pic3" id="pic3" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">是否已领养:</td>
      <td>
		<input type="radio" name="PIsAdopt" value="1" checked> 未领养 
		<input type="radio" name="PIsAdopt" value="2"> 已领养 
		<input type="radio" name="PIsAdopt" value="3"> 退养 
		<input type="radio" name="PIsAdopt" value="4"> 救助人领走 
		<input type="radio" name="PIsAdopt" value="5"> 死亡 
		<input type="radio" name="PIsAdopt" value="6"> 其它 
	  </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">领养日期:</td>
      <td>
      	<input type="text" name="adopt_date" id="adopt_date" value="" size="32" />
      	<img src="../images/calendar.ico" width="25" height="25" onclick="fPopCalendar(event,this,document.getElementById('adopt_date'))" style="cursor:pointer;">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right" valign="top">领养记录:</td>
      <td><textarea name="adopt_history" rows="5" cols="30"></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">领养人姓名:</td>
      <td><input type="text" name="adopter_name" rows="5" cols="30"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">领养人性别:</td>
      <td>
		<input type="radio" name="adopter_sex" value="1" checked> 男 
		<input type="radio" name="adopter_sex" value="2"> 女 
	  </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">领养人身份证:</td>
      <td><input type="text" name="adopter_id" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">领养人工作单位:</td>
      <td><input type="text" name="adopter_workplace" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">领养人电话:</td>
      <td><input type="text" name="adopter_phone" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">领养人地址:</td>
      <td><input type="text" name="adopt_address" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">救助人姓名:</td>
      <td><input type="text" name="res_name" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">救助人性别:</td>
      <td>
		<input type="radio" name="res_sex" value="1" checked> 男 
		<input type="radio" name="res_sex" value="2"> 女 
	  </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">救助人电话:</td>
      <td><input type="text" name="res_phone" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">救助人地址:</td>
      <td><input type="text" name="res_address" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">联系方式:</td>
      <td><input type="text" name="contact_way" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right" valign="top">退养原因:</td>
      <td><textarea name="return_reason" rows="5" cols="30"></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td>
	  	<input type="submit" name="submit" id="submit" value="Submit" />&nbsp;&nbsp;
        <input type="button" id="button" onclick="window.location.href='ShowPetInfo.php'" value="Back">
	  </td>
    </tr>
  </table>
</form>
<p>&nbsp;</p>
</body>
</html>

<script language="JavaScript" type="text/javascript">
function check_form() {
	if (document.getElementById('name').value == '') {
		alert('宠物名 is invalid');
		document.getElementById('name').focus();
		return false;
	}
	if (document.getElementById('guid').value == '') {
		alert('编号 is invalid');
		document.getElementById('guid').focus();
		return false;
	}
	if (document.getElementById('PVariety').value == '') {
		alert('品种 is invalid');
		document.getElementById('PVariety').focus();
		return false;
	}
	if (document.getElementById('res_date').value == '') {
		alert('救助日期 is invalid');
		document.getElementById('res_date').focus();
		return false;
	}
	if (document.getElementById('arr_date').value == '') {
		alert('到达分会日期 is invalid');
		document.getElementById('arr_date').focus();
		return false;
	}
	if (document.getElementById('abundon_reason').value == '') {
		alert('弃养原因 is invalid');
		document.getElementById('abundon_reason').focus();
		return false;
	}
	if (document.getElementById('age').value == '') {
		alert('年龄 is invalid');
		document.getElementById('age').focus();
		return false;
	}
	if (document.getElementById('health_info').value == '') {
		alert('健康情况 is invalid');
		document.getElementById('health_info').focus();
		return false;
	}
	if (document.getElementById('other_info').value == '') {
		alert('性格和其他描述 is invalid');
		document.getElementById('other_info').focus();
		return false;
	}
	if (document.getElementById('story').value == '') {
		alert('故事 is invalid');
		document.getElementById('story').focus();
		return false;
	}
	if (document.getElementById('pic1').value == ''
			&& document.getElementById('pic2').value == ''
			&& document.getElementById('pic3').value == '') {
		alert('至少要传一张图片');
		document.getElementById('pic1').focus();
		return false;
	}
	
	return true;
}
</script>
  